/**
 * 
 */
package neptune.web.client.commands;

import java.util.Map;

import neptune.web.client.WebInterface;
import neptune.web.client.callback.ExperimentDeploymentCallback;
import neptune.web.client.services.experiment.ExperimentService;
import neptune.web.client.view.components.MessageWidget;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * @author oltremago
 * 
 */
public class DeployExperimentCommand implements Command {

	private String expName;
	private Map<String, String[]> allocationMap;

	public DeployExperimentCommand(String expId) {
		expName = expId;
	}

	public DeployExperimentCommand(String expId,
			Map<String, String[]> allocationMap) {
		expName = expId;
		this.allocationMap = allocationMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		if (!Window.confirm("Start experiment '" + expName + "'?"))
			return;

		final PopupPanel popup = WebInterface.showMessageDialog(
				new MessageWidget(MessageWidget.LOADING,
						"Starting experiment..."), false);

		AsyncCallback<String> callBack = new ExperimentDeploymentCallback(
				popup, expName);
		if (allocationMap == null) {
			ExperimentService.Util.getInstance().allocateExperiment(expName,
					callBack);
		} else {
			ExperimentService.Util.getInstance().allocateExperiment(expName,
					allocationMap, callBack);
		}

	}

}
